package com.bjpowernode.controller;

import com.bjpowernode.model.TActivity;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.query.UserQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
public class ActivityController {

    @Resource
    private ActivityService activityService;

    /**
     * 分页查询市场活动
     *
     * @param current
     * @return
     */
    @PreAuthorize(value = "hasAnyAuthority('activity:list','activity:search')")
    @GetMapping(value = "/api/activity")
    public R activityPage(@RequestParam(value = "current", required = false) Integer current, ActivityQuery activityQuery) {
        //required = false表示current参数可以不传；
        //required = true表示current参数必须要传，不传会报错；
        if (current == null) {
            current = 1;
        }

        PageInfo<TActivity> pageInfo = activityService.getActivityByPage(current, activityQuery);

        return R.OK(pageInfo);
    }

    /**
     * 根据id查询市场活动详情信息
     *
     * @return
     */
    @PreAuthorize(value = "hasAuthority('activity:view')")
    @GetMapping(value = "/api/activity/{id}")
    public R activityDetail(@PathVariable(value = "id") Integer id) {
        TActivity tActivity = activityService.getActivityById(id);

        return R.OK(tActivity);
    }

    /**
     * 录入市场活动提交保存
     *
     * @param activityQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('activity:add')")
    @PostMapping(value = "/api/activity")
    public R addActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String jwt) {
        //前端提交过来的是formData，后端怎么接收？
        //1、@RequestParam()注解一个一个字段接收
        //2、request.getParamter()原始的servlet方法一个一个字段接收
        //3、UserQuery 对象一次性接收，把参数封装到对象中了，一次性接收
        activityQuery.setJwt(jwt);
        int save = activityService.saveActivity(activityQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 编辑市场活动提交保存
     *
     * @param activityQuery
     * @return
     */
    @PreAuthorize(value = "hasAuthority('activity:edit')")
    @PutMapping(value = "/api/activity")
    public R editActivity(ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String jwt) {
        //前端提交过来的是formData，后端怎么接收？
        //1、@RequestParam()注解一个一个字段接收
        //2、request.getParamter()原始的servlet方法一个一个字段接收
        //3、UserQuery 对象一次性接收，把参数封装到对象中了，一次性接收
        activityQuery.setJwt(jwt);
        int update = activityService.updateActivity(activityQuery);
        return update >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id删除市场活动
     *
     * @param id
     * @return
     */
    @PreAuthorize(value = "hasAuthority('activity:delete')")
    @DeleteMapping(value = "/api/activity/{id}")
    public R delActivity(@PathVariable(value = "id") Integer id) {
        int del = activityService.delActivityById(id);
        return del >= 1 ? R.OK() : R.FAIL();
    }

    /**
     * 根据id批量删除市场活动
     *
     * @param ids
     * @return
     */
    @PreAuthorize(value = "hasAuthority('activity:delete')")
    @DeleteMapping(value = "/api/activity")
    public R batchDelActivity(@RequestParam(value = "ids") String ids) {
        // ids = "1,3,5,6,11,13,16";
        List<String> idList = Arrays.asList(ids.split(","));

        int batchDel = activityService.batchDelActivityById(idList);
        return batchDel >= idList.size() ? R.OK() : R.FAIL();
    }

    @GetMapping(value = "/api/activity/effective")
    public R effectiveActivity() {
        List<TActivity> tActivityList = activityService.getEffectiveActivity();
        return R.OK(tActivityList);
    }
}
